import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'index',
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
    children:[
      { path:'/home', name:'home', component:() => import('../views/Home.vue')},
      { path: '/infoshow', name:'infoshow', component:() => import('../views/InfoShow.vue')},
      { path:'/fundlist', name:'fundlist', component:() => import('../views/FundList.vue')},
    ]
  },
  {
    path:'/register',
    name:'register',
    component: () => import('../views/Register.vue')
  },
  {
    path:'/login',
    name:'login',
    component:()=> import('../views/Login.vue')
  },
  {
    path:'*',
    name:'/404',
    component:() => import('../views/404.vue')
  }
]

const router = new VueRouter({

  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) =>{
                                           //三目判断
  const isLogin = localStorage.eleToken ? true : false;
  //可以访问登录和注册页面
  if(to.path == '/login' || to.path == '/register'){
    next();
  }else{
    //如果有 token 则继续向下走，否则跳转到登录页
    isLogin ? next() : next('/login')
  }
})

export default router
